/**
 * Created by yqj on 2017/2/3.
 */
import { userResource, dictionaryManagementResource } from '../api'

const LOGIN = ({ commit, dispatch, state }, { loginAccount, loginPassword, verifyCode }) => {
  return userResource.login({loginAccount: loginAccount, loginPassword: loginPassword, verifyCode: verifyCode})
    .then(response => {
      window.localStorage.user_role = state.user_role = response.body.result.role
      console.log(response.body)
      return Promise.resolve(response)
    }, response => {
      return Promise.reject(response)
    })
}

const FETCH_LIST_DATA = ({ commit, dispatch, state }, { type, filterAccountPrefix }) => {
  commit('SET_ACTIVE_TYPE', { type })
  return userResource.getUserByRole({filterRole: 'SUPER_ADMIN', currentPage: state.currentPage, pageSize: state.itemsPerPage, filterAccountPrefix: filterAccountPrefix})
    .then(response => {
      let list = response.body.result.content
      let totalSize = response.body.result.totalSize
      commit('SET_LIST', { type, list })
      commit('SET_TOTAL_SIZE', { totalSize })
    })
}

const FETCH_USER_ROLES = ({ commit, dispatch, state }, { queryOperation }) => {
  return userResource.getUserRoles({queryOperation: queryOperation})
}

const FETCH_USER = ({ commit, dispatch, state }, { id }) => {

}

const FETCH_DICTIONARY = ({ commit, dispatch, state }) => {
  dictionaryManagementResource.getBookType().then(response => {
    let type = 'bookType'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getBrand().then(response => {
    let type = 'brand'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getGrade().then(response => {
    let type = 'grade'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getProvince().then(response => {
    let type = 'province'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getSelfContentType().then(response => {
    let type = 'selfContentType'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getSeries().then(response => {
    let type = 'series'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getSubject().then(response => {
    let type = 'subject'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getTopicType().then(response => {
    let type = 'topicType'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getVersion().then(response => {
    let type = 'version'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
  dictionaryManagementResource.getVolume().then(response => {
    let type = 'volume'
    let list = response.body.result
    commit('SET_DICTIONARY', { type, list })
  })
}

export const actions = {
  LOGIN,
  FETCH_LIST_DATA,
  FETCH_USER_ROLES,
  FETCH_USER,
  FETCH_DICTIONARY
}
